package com.example.avmanagerweb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.avmanagerweb.Utils.DateUtil.JodaTimeUtil;
import com.example.avmanagerweb.Utils.String.StringUtils;
import com.example.avmanagerweb.Utils.VideoProcessing;
import com.example.avmanagerweb.Utils.fileUtil.FileDeletionUtility;
import com.example.avmanagerweb.entity.Devicmsg;
import com.example.avmanagerweb.entity.SysDoc;
import com.example.avmanagerweb.entity.SysUserinfo;
import com.example.avmanagerweb.entity.Videoserverpath;
import com.example.avmanagerweb.entity.vo.DleFile;
import com.example.avmanagerweb.entity.vo.DleFileVo;
import com.example.avmanagerweb.entity.vo.DocCondition;
import com.example.avmanagerweb.entity.vo.FileVo;
import com.example.avmanagerweb.result.RLIST;
import com.example.avmanagerweb.service.DevicmsgService;
import com.example.avmanagerweb.service.SysDocService;
import com.example.avmanagerweb.service.SysUserinfoService;
import com.example.avmanagerweb.service.VideoserverpathService;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2023-06-29
 */
@RestController
@RequestMapping("/sys-doc")
public class SysDocController {
    @Autowired
    private SysDocService sysDocService;
    @Autowired
    private DevicmsgService devicmsgService;
    @Autowired
    private VideoserverpathService videoserverpathService;

    @PostMapping("/getFile")
    public RLIST getFlie(@RequestBody(required = false) FileVo fileVo, HttpServletRequest request) {
        System.out.println(fileVo.toString());
        RLIST sosqryList = sysDocService.getSosqryList(fileVo);
        return  sosqryList;

    }

    @PostMapping(value = "/delFile")
    public RLIST delFile(@RequestBody DleFileVo dleFileVo) {

        List<DleFile> dleFileList = dleFileVo.getDleFileList();
        if(dleFileList == null || dleFileList.size() == 0){
            return RLIST.error().message("参数异常");
        }
        List<Integer> idList = new ArrayList<>();

        for (DleFile dleFile : dleFileList) {
            idList.add(dleFile.getDleId());
            FileDeletionUtility.deleteFile(dleFile.getDleFileName());
        }
        if(idList == null || idList.size() == 0){
            return RLIST.error().message("ID数据为空");
        }

        boolean b = sysDocService.removeByIds(idList);

        if(b){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }

    /**
     * 1.文件保存在服务器，url地址保存在数据库
     * 上传成功之后返回成功保存的url地址
     */
    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    public RLIST upload(MultipartFile file, HttpServletRequest request, Devicmsg devicmsg) {
        System.out.println(devicmsg.toString());
        System.out.println(file.toString());
        Videoserverpath one = videoserverpathService.maxId();
        devicmsg.setStorepath(one.getStorepath());
        devicmsg.setVirtualpath(one.getVirtualpath());
        // 数据库获取存储地址
        String uploadPath = one.getStorepath();
        //生成名字+
        DateTime dateTime = new DateTime();
        String dates = dateTime.toString("yyyyMM");
        String filename = dates;
        System.out.println(devicmsg.getMsgtype() != 0 && file != null);
        if (devicmsg.getMsgtype() != 0 && file != null) {
            if (!file.isEmpty()) {
                // 如果目录不存在则创建
                File uploadDir = new File(uploadPath + filename);
                if (!uploadDir.exists()) {
                    uploadDir.mkdirs();
                }
                String OriginalFilename = file.getOriginalFilename();//获取原文件名
                System.out.println(OriginalFilename);
                String suffixName = OriginalFilename.substring(OriginalFilename.lastIndexOf("."));//获取文件后缀名
                File localFile = new File(uploadPath + filename + "\\" + OriginalFilename);
                try {
                    System.out.println(localFile.toString());
                    file.transferTo(localFile); //把上传的文件保存至本地
                    devicmsg.setMsgcont(filename + "\\" + OriginalFilename);
                    boolean save = devicmsgService.save(devicmsg);
                    if(save){
                        String ftpurl="ftp://127.0.0.1/"+filename + "/" + OriginalFilename;
                        return RLIST.ok().data("data",ftpurl);//上传成功
                    } else {
                        return RLIST.error();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("上传失败");
                    return RLIST.error().message("上传失败");
                }
            } else {
                System.out.println("文件为空");
                return RLIST.error().message("文件为空");
            }
        }
        {
            boolean save = devicmsgService.save(devicmsg);
            if(save){
                return RLIST.ok().data("data",devicmsg.getMsgcont());//上传成功
            } else {
                return RLIST.error();
            }

        }

    }

}

